<?php
/*
+------------------------------------------------------------------------------+
|   Rooster - a plugin by Francis Cheung
|
|	Plugin Support Site: e107.webstartinternet.com
|
|	For the e107 website system visit http://e107.org
|
|	Released under the terms and conditions of the
|	GNU General Public License (http://gnu.org).
+------------------------------------------------------------------------------+
*/
require_once('../../class2.php');
if (!defined('e107_INIT')) { exit(); }
require_once(HEADERF);
include_lan(e_PLUGIN.'roster/languages/'.e_LANGUAGE.'.php');

// +++++++++++++++E107 UPDATABLE VARIABLES BELOW:
// Stop caching for all browsers
session_cache_limiter('nocache');
// Start a session to catch the image list
session_start();
// Render the value of $text in a table.
$title = RS_CORE_00;

// Define inline javascript
$text = '<SCRIPT LANGUAGE="JavaScript1.2" type="text/javascript"> 
function save_data(church_id, job_number, job_dt)
{
        var user_selected=document.getElementById(\'users_dropdown\');
        var id=user_selected.options[user_selected.selectedIndex].id;
        if (id == \'99\')
        {
        	var guest_name=document.getElementById(\'guest\').value;
        	location="roster.php?church_type="+church_id+"&job_id="+job_number+"&job_date="+job_dt+"&user_id="+id+"&guest_name="+guest_name;
        }
        else
        {
        	location="roster.php?church_type="+church_id+"&job_id="+job_number+"&job_date="+job_dt+"&user_id="+id;
        }
}
// End --> ';

$text .= '
function show_hide()
{
	var user_selected=document.getElementById(\'users_dropdown\');
    var id=user_selected.options[user_selected.selectedIndex].id;
    if (id == \'99\')
    {
		document.getElementById(\'guest\').style.display="";
	}
	else
	{
		document.getElementById(\'guest\').style.display="none";
	}
}
</SCRIPT>';

// Get all the ids for setting up the job in db
if (!(isset($_GET['church_id']) && isset($_GET['job_id']) && isset($_GET['job_date'])))
{
	//redirect back to the home page
}

// Get the activity id from job id
if ($sql->db_Select("church_job_tbl","activity_id","id=".$_GET['job_id'],"default"))
{
	list($int_activity_id)=$sql->db_Fetch();
}

$text .= "<table border='1'>";
$text .= "<tr><td>日期:</td><td>".$_GET['job_date']."</td></tr>";
if ($sql->db_Select("church_tbl","type","id=".$_GET['church_id']))
{
	list($str_church_type)=$sql->db_Fetch();
}
else
{
	$str_church_type='ERR';
}
$text .= "<tr><td>教會:</td><td>".$str_church_type."</td></tr>";
if ($sql->db_Select_gen("SELECT a.name FROM ozs_church_activity_tbl a left join ozs_church_job_tbl j ON a.id=j.activity_id WHERE j.id=".$_GET['job_id']))
{
	list($str_activity_name)=$sql->db_Fetch();
}
else
{
	$str_activity_name='ERR';
}
$text .= "<tr><td>事奉:</td><td>".$str_activity_name."</td></tr>";
if ($sql->db_Select("church_job_tbl","name","id=".$_GET['job_id']))
{
	list($str_job_name)=$sql->db_Fetch();
}
else
{
	$str_job_name='ERR';
}
$text .= "<tr><td>職位:</td><td>".$str_job_name."</td></tr>";

// First, we count all the users that are available and have not been selected for this job
if ($sql->db_Select_gen("SELECT count(id) as count_id FROM ozs_church_user_tbl WHERE id NOT IN 
( SELECT user_id FROM ozs_church_roster_tbl WHERE job_date = ".$_GET['job_date'].") AND activity_id = ".$int_activity_id))
{
	list($count_id)=$sql->db_Fetch();
}

// If more than 1 exists, then select them all out and out them in arr_users for the dropdown box
if (($count_id) > 0)
{
	if ($sql->db_Select_gen("SELECT id, name FROM ozs_church_user_tbl WHERE id NOT IN 
	( SELECT user_id FROM ozs_church_roster_tbl WHERE job_date = ".$_GET['job_date'].") AND activity_id = ".$int_activity_id))
	{
		while (list($str_user_id, $str_user_name)=$sql->db_Fetch())
		{
			$arr_users[$str_user_id]=$str_user_name;
		}
	}
	else
	{
		$arr_users=array('ERR');
	}
}

// Now, find the selected user
if ($sql->db_Select_gen("SELECT count(id) as count_id FROM ozs_church_roster_tbl WHERE job_date = ".$_GET['job_date']." AND job_id = ".$_GET['job_id'], true))
{
	list($count_id)=$sql->db_Fetch();
}

// If there's already a user selected for this job, select it out
if ($count_id > 0)
{
	if ($sql->db_Select("church_roster_tbl","user_id, guest_name","job_date = ".$_GET['job_date']." AND job_id = ".$_GET['job_id'],"default"))
	{
		list($str_sel_user_id,$str_sel_guest_name)=$sql->db_Fetch();
		if ($str_sel_user_id == 99)
		{
			$bool_other=true;
			$str_sel_user_name=$str_sel_guest_name;
		}
		else 
		{
			if ($sql->db_Select_gen("SELECT id, name FROM ozs_church_user_tbl WHERE id = ".$str_sel_user_id, true))
			{
				list($str_sel_user_id, $str_sel_user_name)=$sql->db_Fetch();
			}
		}
	}
	else
	{
		$arr_users=array('ERR');
	}
}

$text .= "<tr><td>弟兄/姊妹</td><td><select id='users_dropdown' onChange='show_hide();'>";
//if (isset($arr_sel_users))
//{
//	foreach ($arr_sel_users as $key=>$value)
//	{
if (isset($str_sel_user_id))
{
	if ($bool_other)
	{
		$text .= "<option id='".$str_sel_user_id."' selected='true'>Other</option>";
	}
	else 
	{
		$text .= "<option id='".$str_sel_user_id."' selected='true'>".$str_sel_user_name."</option>";
	}
}
//	}
//}

foreach ($arr_users as $key=>$value)
{
	$text .= "<option id='".$key."'>".$value."</option>";
}
$text .= "<option id='0'>未填滿</option>";
if (!($bool_other))
{
	$text .= "<option id='99'>Other</option>";
}

$text .= "</select></td></tr>";
if ($bool_other)
{
	$text .= "<tr><td colspan='2'><input type='text' id='guest' value='".$str_sel_user_name."' style=''/></tr></td>";
}
else
{
	$text .= "<tr><td colspan='2'><input type='text' id='guest' style='display:none'/></tr></td>";
}
$text .= "</table>";
$str_job_date=ltrim($_GET['job_date'],"\"");
$str_job_date=rtrim($str_job_date,"\"");

$text .= "<input type='button' value='Save' onClick='save_data(".$_GET['church_id'].",".$_GET['job_id'].",".$_GET['job_date'].");'></input>";
$ns -> tablerender($title, $text);

// === End of BODY ===
// use FOOTERF for USER PAGES and e_ADMIN.'footer.php' for admin pages
require_once(FOOTERF);
?>
